/*
 * @Author: C.
 * @Date: 2023-01-30 10:54:54
 */
import { Tag } from 'ant-design-vue';
import Icon from '/@/components/Icon';
import { cloneDeep } from 'lodash-es';
import { projectSettings } from '../../settings/projectSettings';
export interface IEnums {
  value: any;
  label: string;
  icon?: string;
  color?: string;
  [x: string]: any;
}

export function createEnumObject(enums: IEnums[]) {
  return {
    // 获取单个枚举项
    getItem(value, key = null): IEnums | undefined {
      for (const item of enums) {
        if (item[key || 'value'] == value) {
          return item;
        }
      }
    },
    // 获取所需的key值集合
    getColums(key): any[] {
      return enums.map((item) => item[key]);
    },
    // 获取单个值
    getColum(column, key, value) {
      const item = this.getItem(value, key);
      if (item) {
        // h(Tag, { color: item.color},item[column]);
        // return h(Tag, { color: item.color }, item[column]);
        return item[column];
      } else {
        return projectSettings.dataPlaceholder;
      }
    },
    // 获取label集合
    getLabels(): any[] {
      return this.getColums('label');
    },
    // 获取值集合
    getValues(): any[] {
      return this.getColums('value');
    },
    // 获取单个label
    getLabel(value, key = null): any {
      return this.getColum('label', key || 'value', value);
    },
    // 获取单个值
    getValue(value, key = null) {
      return this.getColum('value', key || 'label', value);
    },
    getTag(value) {
      const item = this.getItem(value);
      if (item) {
        return (
          <Tag color={item.color}>
            {item.icon ? <Icon icon={item.icon}></Icon> : undefined}
            {item.label}
          </Tag>
        );
      } else {
        return projectSettings.dataPlaceholder;
      }
    },
    // 获取原始枚举 如果enums的子项中存在引用类型 回造成无限循环
    getEnums(): IEnums[] {
      return enums.map((item) => {
        return {
          value: item.value,
          label: item.label,
          color: item.color,
          icon: item.icon,
        };
      });
    },
    // 获取首个默认值
    getDefaultValue(key = 'value', index = 0): string {
      return enums[index][key];
    },
  };
}
